package org.cinvestav.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class DOCXtoText {

	public DOCXtoText() {

		File file = null;
		PrintStream outfile = null;
		WordExtractor extractor = null;
		try {
			file = new File(
					"C:\\Users\\Xeus\\workspace\\toText\\ExFiles\\cvcopei2010.doc");
			outfile = new PrintStream(
					new FileOutputStream(
							"C:\\Users\\Xeus\\workspace\\toText\\ExFiles\\cvcopei2010.txt"));
			FileInputStream fis = new FileInputStream(file.getAbsolutePath());
			HWPFDocument document = new HWPFDocument(fis);
			extractor = new WordExtractor(document);
			String[] fileData = extractor.getParagraphText();
			for (int i = 0; i < fileData.length; i++) {
				if (fileData[i] != null) {
					// Print content file to Console
					// System.out.println(fileData[i]);
					outfile.print(fileData[i]);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (outfile != null)
				outfile.close();
		}
	}

	public void getDOCtoText(String in, String out) {
		File file = null;
		PrintStream outfile = null;
		WordExtractor extractor = null;
		try {
			file = new File(in);
			outfile = new PrintStream(new FileOutputStream(out));
			FileInputStream fis = new FileInputStream(file.getAbsolutePath());
			HWPFDocument document = new HWPFDocument(fis);
			extractor = new WordExtractor(document);
			String[] fileData = extractor.getParagraphText();
			for (int i = 0; i < fileData.length; i++) {
				if (fileData[i] != null) {
					// Print content file to Console
					// System.out.println(fileData[i]);
					outfile.print(fileData[i]);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (outfile != null)
				outfile.close();
		}
	}

	public void getDOCXMLtoText(String in, String out) {
		PrintStream outfile = null;
		try {
			FileInputStream fis = new FileInputStream(in);
			outfile = new PrintStream(
					new FileOutputStream(out));

			XWPFWordExtractor oleTextExtractor = new XWPFWordExtractor(
					new XWPFDocument(fis));
			// Print content file to Console
			// System.out.print(oleTextExtractor.getText());
			outfile.print(oleTextExtractor.getText());

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (outfile != null)
				outfile.close();
		}
	}

	public static void main(String[] args) {
		//DOCXMLtoText();
	}
}
